package ru.nsu.fit.client.core.commands;

import ru.nsu.fit.client.core.log_manager.realization.KernelLogManager;
import ru.nsu.fit.client.core.response.KernelResponse;
import ru.nsu.fit.client.core.soap.send_answer.SendAnswer;
import ru.nsu.fit.client.core.soap.send_answer.SendAnswerOutput;

public class SendAnswerCommand implements  IClientCommand {
    private final int answerId;
    private final long userSessionId;
    private final int questionId;
    private final String serverUrl;

    public SendAnswerCommand(final String serverUrl, final long userSessionId, final int questionId, final int answerId)
    {
        this.userSessionId = userSessionId;
        this.answerId = answerId;
        this.questionId = questionId;
        this.serverUrl = serverUrl;
    }

    @Override
    public KernelResponse execute() {
        try
        {
            KernelLogManager.print("Sending answer...");
            SendAnswerOutput output = SendAnswer.execute(serverUrl, userSessionId, questionId, answerId);

            KernelLogManager.print("Sending answer - got response!");
            KernelResponse response = new KernelResponse(false);
            response.setErrorCode(output.getErrorCode());
            response.setErrorText(output.getErrorMessage());

            return response;
        }
        catch (Exception e)
        {
            KernelLogManager.printf("Sending answer - web service error: %s", e.getMessage());
            KernelResponse response = new KernelResponse(false);
            response.setErrorText(e.getMessage());
            return response;
        }
    }
}
